Skip to content

compile: parallel module compile by default, with a make jobserver client - #4782

Merged
aleksisch merged 7 commits into
masterfrom
aleksisch/jobserver-support
Nov 26, 2026
Merged

aleksisch merged 7 commits into
masterfrom
aleksisch/jobserver-support

Conversation

@aleksisch

Copy link
Copy Markdown
Collaborator

Behavior change: required modules now compile on all cores by default - a host that wants one thread sets CodeOfPolicies.compile_threads = 1.

Why. The parallel module compile was opt-in, and a parallel compiler under make -j or a parallel test runner starts N processes that each use every core.

What changes.

  • A GNU make jobserver client (src/misc/jobserver.cpp) reads --jobserver-auth from MAKEFLAGS (make 4.4 fifo, inherited pipe fds, Windows semaphore); the compile DAG and the split JIT emit run on one thread plus the tokens the pool grants.
  • CodeOfPolicies.compile_threads defaults to 0, the JobQue thread count.
  • daslang keeps one thread for -aot, -use-aot, -exe and -lib; -compile-threads still overrides the last three.

Observable behavior.

  • daslang x.das compiles required modules on 1 thread -> on all cores.
  • Under make -j3 with a + recipe -> the compile takes at most 2 extra threads, and gives them back.
  • -aot / -use-aot / -exe / -lib -> unchanged, one thread.

Where to look. JobserverTokens in src/misc/jobserver.cpp, its two call sites (compileModules, jit_par_emit_run), and the new default in code_of_policies.h.

Validation, claims, ledger

Validation

  • Local, Linux, make 4.3 (pipe fds): under make -j3 / -j16 with -compile-threads 8, strace shows 2 / 7 tokens read from the pool and the same count written back; a recipe without + gets no fds and the client stays off.
  • Full preflight, the AOT build and the auditor rounds were not run; tests/language under dastest (now parallel by default) passed before the rebase.

Claims - stated, not tested

  • The Windows semaphore path builds in CI but no Windows run used a real make jobserver.
  • macOS has no /proc, so the pipe-fd form turns the client off there; the make 4.4 fifo form still works.

Not done

  • A jobserver server in dastest, so its worker processes share one pool.
  • Taking tokens one by one as workers start: tokens are taken once, up front.

@aleksisch
aleksisch force-pushed the aleksisch/jobserver-support branch 12 times, most recently from 2476bec to 4bf457d Compare November 26, 2026 01:22
@aleksisch
aleksisch requested a review from borisbat November 26, 2026 05:15
A job learns that its module nests a compile only when the module's
compile ends. From that walk position on no job starts, later results
that finished are dropped, and the main thread compiles those modules in
walk order with the whole sequential pipeline. The deferred macro
context build, which ran after the module's GC finalizer had shared its
types, is gone.

A comment reader reads every module parsed after it, required or not, so
a module that installs one has to be declared `!inscope`, and is an
error otherwise. The require scan reads `!inscope` from the module line,
and every module after such a module in walk order depends on it in the
DAG: it is parsed after the reader exists, and still runs in parallel
with the others. A parse that takes a reader holds one lock for the
whole parse, so parallel parses no longer interleave on the reader's
state (the lint crashed in rst_comment).
fork_debug_agent_context creates a second context and runs code in it,
like the builtins already marked nestsCompile. A macro module that calls
it - daslib/profiler's installer - now builds its macro context on the
main thread after the DAG jobs; built on a worker, it installed its
thread-local debug agent there, and that agent outlived the process.
run_split_emit in llvm_jit_run.das optimizes and emits each partition on
jobque jobs, which call the LLVM C API directly; jobs and results travel
through channels, so no string crosses a context. jit_par_emit_begin/
add/run and their std::thread pool leave module_jit.cpp. A job runs on a
clone of the JIT context, so llvm_macro's LLVMShutdown finalizer now
skips job and thread clones: a dying job no longer shuts LLVM down under
the jobs still running. Same codegen times on the lint (25 partitions).
Each module of the compile DAG and each partition of the split JIT emit
holds a token of the build's GNU make jobserver while it runs (MAKEFLAGS
--jobserver-auth: a make 4.4 fifo, inherited pipe fds, or a Windows
semaphore). Jobserver::acquire returns the process's implicit token or a
pool token, or waits; the JobserverToken gives it back. das reaches it
through jobque's with_jobserver and with_jobserver_token. With no
jobserver advertised acquire returns at once, as before.
@aleksisch
aleksisch force-pushed the aleksisch/jobserver-support branch from 4bf457d to 8c1e2bf Compare November 26, 2026 08:24
A read-only walk is a ConstVisitor: the printer keeps the levels that
place its brackets in a table of its own, and the inliner's clone shape
stores no children back. ConstVisitor gains the program-level hooks the
printer needs (constVisitProgram, constVisitFunction). A module freezes
with its types' alias caches filled, a dynamic module loaded on demand
freezes like the startup ones, and folding never stamps access_fold on a
frozen global. A wasm build without threads compiles in walk order.
compile_threads still defaults to 1; -compile-threads turns jobs on.
On Windows popen runs the line through cmd.exe, which drops the first
and the last quote of a line that starts with one. The child command
starts with the quoted daslang path, so the path broke and the test
failed with "The filename, directory name, or volume label syntax is
incorrect". The line is wrapped in one more pair of quotes there.
@aleksisch
aleksisch force-pushed the aleksisch/jobserver-support branch from b75fd27 to cdf4abd Compare November 26, 2026 15:07
The POSIX pool calls dup and reads /proc/self/fd, and the PS4/PS5 libc
has neither, so jobserver.cpp did not compile there. On DAS_TARGET_PS
the pool is a stub: open returns null, and Jobserver falls back to its
own limit, as it does when no make jobserver is in the environment.
@aleksisch
aleksisch merged commit b7cc855 into master Nov 26, 2026
66 checks passed
@aleksisch
aleksisch deleted the aleksisch/jobserver-support branch November 26, 2026 16:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant